package com.manager.service.imp;

import com.common.result.BaseResult;
import com.common.utils.JsonUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manager.dao.GoodsCategoryMapper;
import com.manager.dao.GoodsMapper;
import com.manager.pojo.Goods;
import com.manager.pojo.GoodsCategory;
import com.manager.pojo.GoodsCategoryExample;
import com.manager.pojo.GoodsExample;
import com.manager.service.goodCategoryService;
import com.manager.vo.GoodsCategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import sun.swing.StringUIClientPropertyKey;

import java.util.ArrayList;
import java.util.List;
@Service
public class goodCategoryServiceImpl implements goodCategoryService {
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Value("${goods.category.list.key}")
    private String goodsCategoryListKey;

    @Override
    public List<GoodsCategory> selectCategoryTopList() {
        //创建查询对象
        GoodsCategoryExample goodsCategoryExample = new GoodsCategoryExample();
        //设置查询条件,parent_id=0
        goodsCategoryExample.createCriteria().andParentIdEqualTo((short)0);
        //查询
        return goodsCategoryMapper.selectByExample(goodsCategoryExample);
    }

    @Override
    public List<GoodsCategory> selectParentByParentId(Short parentId) {
        //创建查询条件
        GoodsCategoryExample goodsCategoryExample=new GoodsCategoryExample();
        //设置查询条件->传入父id
        goodsCategoryExample.createCriteria().andParentIdEqualTo(parentId);
        //根据条件进行查询
        return goodsCategoryMapper.selectByExample(goodsCategoryExample);
    }

    /**
     * 商品列表
     * @return
     */

    @Override
    public List<GoodsCategoryVo> selectCategoryForView() {
        //获取字符串操作接口
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        String gcvListJson = valueOperations.get(goodsCategoryListKey);
        if (!StringUtils.isEmpty(gcvListJson)){
           return JsonUtil.jsonToList(gcvListJson,GoodsCategoryVo.class);
        }
        //创建查询条件
        GoodsCategoryExample goodsCategoryExample=new GoodsCategoryExample();
        //设置查询条件 where parentId=0 and level=1
        goodsCategoryExample.createCriteria().andParentIdEqualTo((short)0).andLevelEqualTo((byte) 1);
        //进行查询
        List<GoodsCategory> gcList = goodsCategoryMapper.selectByExample(goodsCategoryExample);
        ArrayList<GoodsCategoryVo> gcvList01 = new ArrayList<>();

        for (GoodsCategory gc01 : gcList) {
           GoodsCategoryVo gcv01=new GoodsCategoryVo();
            BeanUtils.copyProperties(gc01,gcv01);
            //清空查询条件
            goodsCategoryExample.clear();
            //再次设置查询条件对二级分类进行查询
            goodsCategoryExample.createCriteria().andParentIdEqualTo((short) 2).andLevelEqualTo((byte) 2);
            //对查询条件进行查询
            List<GoodsCategory> gcList02 = goodsCategoryMapper.selectByExample(goodsCategoryExample);
            ArrayList<GoodsCategoryVo> gcvList02 = new ArrayList<>();

            for (GoodsCategory gc02 : gcList02) {
                GoodsCategoryVo gcv02=new GoodsCategoryVo();
                //存储二级分类信息
                BeanUtils.copyProperties(gc02,gcv02);
                gcvList02.add(gcv02);
                //清空查询条件
                goodsCategoryExample.clear();
                //再次设置查询条件进行三级查询
                goodsCategoryExample.createCriteria().andParentIdEqualTo((short) 3).andLevelEqualTo((byte) 3);
                //对查询条件进行查询
                List<GoodsCategory> gcList03 = goodsCategoryMapper.selectByExample(goodsCategoryExample);
                ArrayList<GoodsCategoryVo> gcvList03 = new ArrayList<>();
                for (GoodsCategory gc03 : gcList03) {
                    GoodsCategoryVo gcv03=new GoodsCategoryVo();
                    BeanUtils.copyProperties(gc03,gcv03);
                    gcvList03.add(gcv03);
                }
                //把三级分类放入二级分类对象里
                gcv02.setChildren(gcvList03);
                gcvList02.add(gcv02);
            }
            gcv01.setChildren(gcvList02);
            gcvList01.add(gcv01);

        }
        valueOperations.set(goodsCategoryListKey,JsonUtil.object2JsonStr(gcvList01));
        return gcvList01;
    }

    /**
     * 商品分类->新增分类->保存分类
     * @param goodsCategory
     * @return
     */
    @Override
    public int categorySave(GoodsCategory goodsCategory) {
        //删除redis缓存的数据
        redisTemplate.delete("goods*");
        return goodsCategoryMapper.insertSelective(goodsCategory);
    }

    /**
     * 查询所有分类
     * @return
     */
    @Override
    public List<GoodsCategory> categoryList() {

        return goodsCategoryMapper.selectByExample(new GoodsCategoryExample());
    }

    @Override
    public BaseResult selectGoodsListByPage(Goods goods, Integer pageNum, Integer pageSize) {
        //创建查询对象
        GoodsExample goodsExample=new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        PageHelper.startPage(pageNum,pageSize);
        //如果id不为空就把id作为查询条件
        if (goods.getGoodsId()!=null&&goods.getGoodsId()!=0){
            criteria.andCatIdEqualTo(goods.getCatId());
        }
        if (goods.getBrandId()!=null&&goods.getBrandId()!=0){
            criteria.andBrandIdEqualTo(goods.getBrandId());
        }
        //对关键字进行查询
        if (!StringUtils.isEmpty(goods.getGoodsName())){
            criteria.andGoodsNameLike("%"+goods.getGoodsName()+"%");
        }
        //对查询条件进行查询
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
        //对结果进行处理
        if (!CollectionUtils.isEmpty(goodsList)){
            PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsList);
            return BaseResult.success(goodsPageInfo);
        }
        return BaseResult.error();
    }


}
